package controller

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strings"
)

func (app *Application) My(w http.ResponseWriter, r *http.Request)  {

	// 注册用户
	id := "user:001"
	name := "zhipeng"
	role := "1"
	//
	_, err := app.Setup.My(id, name, role, "test")
	if err != nil{

	}
	fmt.Printf("注册用户成功")

	data := make(map[string]string)
	data["data"] = "zhipeng"

	ShowView(w, r, "my.html", data)
}

func (app *Application) Test(w http.ResponseWriter, r *http.Request)  {

	// 获取参数
	param := r.FormValue("param")

	// 拆分
	arr := strings.Split(param, "-")

	fmt.Println(arr)

	// 返回数据
	ret := make(map[string]string)
	ret["code"] = "0"
	ret["data"] = "0"

	ret_json,_ := json.Marshal(ret)

	io.WriteString(w, string(ret_json))
}

func (app *Application) Invoke(w http.ResponseWriter, r *http.Request)  {

	// 获取参数
	param := r.FormValue("param")

	// 拆分
	array := strings.Split(param, "-")

	txid, _ := app.Setup.Invoke(array)

	// 返回数据
	ret := make(map[string]string)
	ret["code"] = "0"
	ret["data"] = txid

	ret_json,_ := json.Marshal(ret)

	io.WriteString(w, string(ret_json))
}


func (app *Application) Query(w http.ResponseWriter, r *http.Request)  {

	// 获取参数
	param := r.FormValue("param")

	// 拆分
	array := strings.Split(param, "-")

	result , _ := app.Setup.Query(array)

	value := string(result[:])

	fmt.Println(value)

	// 返回数据
	ret := make(map[string]string)
	ret["code"] = "0"
	ret["data"] = value

	ret_json,_ := json.Marshal(ret)

	io.WriteString(w, string(ret_json))
}

func (app *Application) Page(w http.ResponseWriter, r *http.Request)  {
	params := r.URL
	split := strings.Split(params.RawQuery, "=")

	ShowView(w, r,  split[1] + ".html", nil)
}